package com.novabluetooth.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.blankj.utilcode.util.GsonUtils;
import com.health.R;
import com.health.tablebean.SBCL_DATA;
import com.health.viewscontrols.NovaApplication;
import com.novabluetooth.services.BluetoothConstant;
import com.novabluetooth.views.twj.PwBean;
import com.peng.ppscalelibrary.BleManager.Model.BleEnum;
import com.peng.ppscalelibrary.BleManager.Model.LFPeopleGeneral;
import com.peng.ppscalelibrary.BleManager.util.PPUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备数据结果
 * 
 * @author nova
 *
 */
public class ExamHistoryWeightAdaptor extends BaseAdapter {

	private static final String TAG = "ExamHistoryAdaptor";
	private List<SBCL_DATA> list;
	private Context context;
	private String flag;

	public ExamHistoryWeightAdaptor(List<SBCL_DATA> list, Context context, String flag) {
		super();
		this.list = list;
		this.context = context;
		this.flag = flag;
	}

	@Override
	public int getCount() {
		if (list == null) {
			return 0;
		}
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh;
		SBCL_DATA data = null;

		data = list.get(position);

		if (convertView == null) {
			convertView = View.inflate(context, R.layout.exam_history_weight_item, null);
			vh = new ViewHolder();
			vh.tv1 = (TextView) convertView.findViewById(R.id.exam_history_tv1);
			vh.tv2 = (TextView) convertView.findViewById(R.id.exam_history_tv2);
			vh.tv3 = (TextView) convertView.findViewById(R.id.exam_history_tv3);
			vh.tv4 = (TextView) convertView.findViewById(R.id.exam_history_tv4);
			vh.tv5 = (TextView) convertView.findViewById(R.id.exam_history_tv5);
			vh.tv6 = (TextView) convertView.findViewById(R.id.exam_history_tv6);
			vh.tv7 = (TextView) convertView.findViewById(R.id.exam_history_tv7);
			vh.tv8 = (TextView) convertView.findViewById(R.id.exam_history_tv8);
			vh.tv9 = (TextView) convertView.findViewById(R.id.exam_history_tv9);
			vh.tv10 = (TextView) convertView.findViewById(R.id.exam_history_tv10);
			vh.tv11 = (TextView) convertView.findViewById(R.id.exam_history_tv11);
			vh.tv12 = (TextView) convertView.findViewById(R.id.exam_history_tv12);
			vh.tv13 = (TextView) convertView.findViewById(R.id.exam_history_tv13);
			vh.tv14 = (TextView) convertView.findViewById(R.id.exam_history_tv14);
			vh.tv15 = (TextView) convertView.findViewById(R.id.exam_history_tv15);
			vh.tv16 = (TextView) convertView.findViewById(R.id.exam_history_tv16);
			vh.tv17 = (TextView) convertView.findViewById(R.id.exam_history_tv17);
			vh.tv18 = (TextView) convertView.findViewById(R.id.exam_history_tv18);
			vh.tv19 = (TextView) convertView.findViewById(R.id.exam_history_tv19);
			vh.tv20 = (TextView) convertView.findViewById(R.id.exam_history_tv20);
			vh.tv21 = (TextView) convertView.findViewById(R.id.exam_history_tv21);
			vh.tv22 = (TextView) convertView.findViewById(R.id.exam_history_tv22);
			vh.tv23 = (TextView) convertView.findViewById(R.id.exam_history_tv23);
			vh.tv24 = (TextView) convertView.findViewById(R.id.exam_history_tv24);
			vh.timeTv = (TextView) convertView.findViewById(R.id.exam_history_time);
			TextView[] tvs = { vh.tv1, vh.tv2, vh.tv3, vh.tv4, vh.tv5, vh.tv6, vh.tv7, vh.tv8,
					vh.tv9, vh.tv10, vh.tv11,vh.tv12,vh.tv13,vh.tv14,vh.tv15,vh.tv16,vh.tv17,vh.tv18,
					vh.tv19,vh.tv20, vh.tv21,vh.tv22,vh.tv23,vh.tv24,vh.timeTv };
//			showText(21, tvs);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		try {
			Log.e(TAG, "getView:11111 " );
			LFPeopleGeneral bodyDataModel = GsonUtils.fromJson(data.getCL_VALUE(), LFPeopleGeneral.class);
			String weightStr = PPUtil.getWeight(BleEnum.BleUnit.BLE_UNIT_KG, bodyDataModel.lfWeightKg);
			Log.e(TAG, "getView:11111 " + bodyDataModel.toString());
//				{ "体重", "BMI", "肌肉量", "脂肪率", "骨量", "皮下脂肪", "水分率",
//						"蛋白质", "健康评估","肥胖等级","健康等级",
//						"身体年龄","理想体重",身体类型,身体得分，肌肉率，脂肪量
//						"检测时间" };
			//体重
			vh.tv1.setText(weightStr);
			//BMI
			vh.tv2.setText(Double.parseDouble(String.format("%.2f",bodyDataModel.getLfBMI())) + "");
			//肌肉量
			vh.tv3.setText(Double.parseDouble(String.format("%.2f",bodyDataModel.getLfMuscleKg())) + "%");
			//脂肪率
			vh.tv4.setText(Double.parseDouble(String.format("%.2f",bodyDataModel.getLfBodyfatPercentage())) + "%");
			//骨量含量
			vh.tv5.setText(Double.parseDouble(String.format("%.2f",bodyDataModel.getLfBoneKg())) + "kg");
			//皮下脂肪
			vh.tv6.setText(Double.parseDouble(String.format("%.2f",bodyDataModel.getLfVFPercentage())) + "%");
			//水分率
			vh.tv7.setText(Double.parseDouble(String.format("%.2f",bodyDataModel.getLfWaterPercentage())) + "%");
			//蛋白质
			vh.tv8.setText(Double.parseDouble(String.format("%.2f",bodyDataModel.getLfProteinPercentage())) + "%");
			//健康评估
			vh.tv9.setText(bodyDataModel.countHealthLevel() + "");
			//肥胖等级
			vh.tv10.setText(bodyDataModel.getLfFatLevel() + "");
			//健康等级
			vh.tv11.setText(bodyDataModel.countHealthReport() + "");
			//身体年龄
			vh.tv12.setText(bodyDataModel.getLfBodyAge() + "岁");
			//理想体重
			vh.tv13.setText(Double.parseDouble(String.format("%.2f",bodyDataModel.getLfIdealWeightKg())) + "kg");
			//身体类型
			vh.tv14.setText(bodyDataModel.getLfBodyType() + "");
			//身体得分
			vh.tv15.setText(bodyDataModel.getLfBodyScore() + "");
			//肌肉率
			vh.tv16.setText(Double.parseDouble(String.format("%.2f",bodyDataModel.getLfMusclePercentage())) + "%");
			//脂肪量
			vh.tv17.setText(Double.parseDouble(String.format("%.2f",bodyDataModel.getLfBodyfatKg())) + "kg");
			//去脂体重
			vh.tv18.setText(Double.parseDouble(String.format("%.2f",bodyDataModel.getLfLoseFatWeightKg())) + "kg");
			//体重控制
			vh.tv19.setText(Double.parseDouble(String.format("%.2f",bodyDataModel.getLfControlWeightKg())) + "kg");
			//脂肪控制量
			vh.tv20.setText(Double.parseDouble(String.format("%.2f",bodyDataModel.getLfFatControlKg())) + "kg");
			//骨骼肌率
			vh.tv21.setText(Double.parseDouble(String.format("%.2f",bodyDataModel.getLfBonePercentage())) + "%");
			//肌肉控制量
			vh.tv22.setText(Double.parseDouble(String.format("%.2f",bodyDataModel.getLfBodyMuscleControlKg())) + "kg");
			//内脏脂肪等级
			vh.tv23.setText(bodyDataModel.getLfVFAL() + "");
			//BMR 基础代谢
			vh.tv24.setText(bodyDataModel.getLfBMR() + "");
			vh.timeTv.setText(data.getCL_TIME());
		} catch (Exception e) {
			e.printStackTrace();
			Log.e(TAG, "getView:Exception  " +e.toString() );
		}
		return convertView;
	}

	class ViewHolder {
		TextView tv1;
		TextView tv2;
		TextView tv3;
		TextView tv4;
		TextView tv5;
		TextView tv6;
		TextView tv7;
		TextView tv8;
		TextView tv9;
		TextView tv10;
		TextView tv11;
		TextView tv12;
		TextView tv13;
		TextView tv14;
		TextView tv15;
		TextView tv16;
		TextView tv17;
		TextView tv18;
		TextView tv19;
		TextView tv20;
		TextView tv21;
		TextView tv22;
		TextView tv23;
		TextView tv24;
		TextView timeTv;
	}

	private void showText(int count, TextView[] tvs) {
		if (count < 1 || count > 12) {
			return;
		}
		for (TextView textView : tvs) {
			if (textView.getVisibility() == View.VISIBLE) {
				textView.setVisibility(View.GONE);
			}
		}

		for (int i = 0; i < count - 1; i++) {
			TextView textView = tvs[i];
			if (textView.getVisibility() != View.VISIBLE) {
				textView.setVisibility(View.VISIBLE);
			}
		}
		tvs[tvs.length - 1].setVisibility(View.VISIBLE);

	}

}
